/* Software License Agreement (BSD License)
 *
 * Copyright (c) 2013, University of Southern California
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 *  * Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 *  * Redistributions in binary form must reproduce the above
 *    copyright notice, this list of conditions and the following
 *    disclaimer in the documentation and/or other materials provided
 *    with the distribution.
 *  * Neither the name of University of Southern California nor the
 *    names of its contributors may be used to endorse or promote
 *    products derived from this software without specific prior
 *    written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "ros/ros.h"

#include "geometry_msgs/Twist.h"
#include "tf/transform_broadcaster.h"
#include "nav_msgs/Odometry.h"
#include "sensor_msgs/LaserScan.h"

#include "kipr_link_sensors/Status.h"
#include "kipr_link_create/Status.h"
#include "kipr_link_create/cmd_vel.h"

void kipr_link_status_callback(const kipr_link_sensors::Status::ConstPtr& msg)
{
 // TBD: Store the sensor values in a global variable 
}

void kipr_link_create_status_callback(const kipr_link_create::Status::ConstPtr& msg)
{
  // TBD: Store the sensor values in a global variable
}

void geometry_msgs_twist_callback(const geometry_msgs::Twist::ConstPtr& msg)
{
  static ros::NodeHandle n;
  static ros::Publisher pub_link_create_cmd_vel = n.advertise<kipr_link_create::cmd_vel>("kipr_link/create/cmd_vel", 1000);

  kipr_link_create::cmd_vel cmd_vel_msg;
  
  // TBD: fill the cmd_vel_msg
  
  pub_link_create_cmd_vel.publish(cmd_vel_msg);
}

int main(int argc, char **argv)
{
  ros::init(argc, argv, "robot_driver");

  ros::NodeHandle n;

  // Create the subscriber
  ros::Subscriber sub_link_status = n.subscribe("kipr_link/Status", 1000, kipr_link_status_callback);
  ros::Subscriber sub_link_create_status = n.subscribe("kipr_link/create/Status", 1000, kipr_link_create_status_callback);
  ros::Subscriber sub_cmd_vel = n.subscribe("cmd_vel", 1000, geometry_msgs_twist_callback);

  // Create the publisher
  ros::Publisher pub_odom = n.advertise<nav_msgs::Odometry>("odom", 1000);
  ros::Publisher pub_scan = n.advertise<sensor_msgs::LaserScan>("scan", 1000);

  // Main loop frequency in Hz
  ros::Rate loop_frequency(10);

  while (ros::ok())
  {
    // Publish "odom"
    nav_msgs::Odometry odom_msg;

    // TBD: Fill the message

    pub_odom.publish(odom_msg);

    // Publish "scan"
    sensor_msgs::LaserScan scan_msg;

    // TBD: Fill the message

    pub_scan.publish(scan_msg);

    // Publish "tf"
    static tf::TransformBroadcaster transform_br;
    tf::Transform transform;

    // TBD: transform.setOrigin(tf::Vector3(x, y, 0.0));
    // TBD: transform.setRotation( tf::Quaternion(msg->theta, 0, 0) );

    transform_br.sendTransform(tf::StampedTransform(transform, ros::Time::now(), "world", "robot"));

    ros::spinOnce();

    loop_frequency.sleep();
  }

  return 0;
}

